package com.stock_t.controller;

import com.stock_t.dto.ApiResponse;
import com.stock_t.dto.StockStatisticsResponse;
import com.stock_t.service.StockStatisticsService;
import com.stock_t.service.StockTradingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 股票统计控制器
 */
@RestController
@RequestMapping("/api/stock")
public class StockStatisticsController {

    @Autowired
    private StockStatisticsService stockStatisticsService;

    @Autowired
    private StockTradingService stockTradingService;

    /**
     * 根据股票代码获取统计信息
     */
    @GetMapping("/statistics/{stockCode}")
    public ApiResponse<StockStatisticsResponse> getStockStatistics(@PathVariable String stockCode) {
        try {
            StockStatisticsResponse response = stockStatisticsService.getStockStatistics(stockCode);
            return ApiResponse.success(response);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取股票统计信息失败: " + e.getMessage());
        }
    }

    /**
     * 根据股票代码获取最新数据（对应StockStatisticsServiceImpl.getLatestData方法）
     */
    @GetMapping("/statistics/latest/{stockCode}")
    public ApiResponse<StockStatisticsResponse.LatestData> getLatestData(@PathVariable String stockCode) {
        try {
            StockStatisticsResponse.LatestData latestData = stockStatisticsService.getLatestData(stockCode);
            return ApiResponse.success(latestData);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取股票最新数据失败: " + e.getMessage());
        }
    }

        /**
     * 获取所有股票的最新数据
     */
    @GetMapping("/statistics/latest/all")
    public ApiResponse<List<Map<String, Object>>> getAllLatestData() {
        try {
            // 获取所有股票列表
            List<Map<String, String>> stockList = stockTradingService.getStockList();
            List<Map<String, Object>> allLatestData = new ArrayList<>();
            
            // 为每只股票获取最新数据
            for (Map<String, String> stock : stockList) {
                String stockCode = stock.get("stockCode");
                String stockName = stock.get("stockName");
                
                try {
                    StockStatisticsResponse.LatestData latestData = stockStatisticsService.getLatestData(stockCode);
                    
                    // 创建包含股票代码和名称的响应数据
                    Map<String, Object> stockData = new HashMap<>();
                    stockData.put("stockCode", stockCode);
                    stockData.put("stockName", stockName);
                    stockData.put("latestData", latestData);
                    
                    allLatestData.add(stockData);
                } catch (Exception e) {
                    // 单只股票失败不影响整体，记录日志并继续
                    System.err.println("获取股票 " + stockCode + " 最新数据失败: " + e.getMessage());
                }
            }
            
            return ApiResponse.success("获取所有股票最新数据成功", allLatestData);
        } catch (Exception e) {
            return ApiResponse.error(500, "获取所有股票最新数据失败: " + e.getMessage());
        }
    }
}